package other;

import java.io.*;
import java.util.regex.Pattern;

/**
 * description
 *
 * @author 朱宗刚
 * @version 1.0.0
 * @date 2022/3/1
 */

public class MergeLine {
    public static void main(String[] args) {
        String inFile = "E:\\tmp\\input.txt";
        String outFile ="E:\\tmp\\output.txt";
        String regex = "^\\d+\\t\\d*";
        try(BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(inFile)));
            BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(outFile)))) {
            Pattern pattern = Pattern.compile(regex);
            StringBuilder builder = new StringBuilder();
            String line = "";
            String preLine = "";
            while((line = reader.readLine()) != null) {
                if(pattern.matcher(line).find()) {
                    if(preLine.length() != 0) {
                        writer.write(preLine + "\n");
                    }

                    builder.setLength(0);
                    builder.append(line);
                    preLine = builder.toString();
                } else {
                    builder.append(line.trim());
                    preLine = builder.toString();
                }
            }
            writer.write(preLine + "\n");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
